package com.jiazhong.cloud.seata.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiazhong.cloud.seata.order.bean.Order;
import com.jiazhong.cloud.seata.order.feign.StockFeign;
import com.jiazhong.cloud.seata.order.mapper.OrderMapper;
import com.jiazhong.cloud.seata.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private StockFeign stockFeign;

    @GlobalTransactional
    @Transactional(rollbackFor = Exception.class)
    public int saveOrder(Order order) {
        // 1. 添加订单
        boolean b = save(order);
        // 2. 更新库存
        String s = stockFeign.update(order.getGid(), order.getNumber());
        return b ? 1 : 0;
    }
}
